package com.training.java.core.homework02;

/**
 * Calculator program
 * This program simulates a Windows calculator as closely as possible.

 * @author Shovan
 * 
 */
import java.util.InputMismatchException;
import java.util.Scanner;

public class CalculatorDemo {

	public static void main(String[] args) {

		Calculator calculator = new Calculator(0, 0, 0, 0);
		double amount1 = 0, amount2 = 0;
		char operator = 'a';
		boolean keepTrying;

		Scanner input = new Scanner(System.in);

		// Since this is meant to resemble a Windows calculator or a hand held
		// calculator, in the first part of the
		// program 2 numbers are being accepted and operated upon. Once that is
		// done, results are stored in the
		// calculator. Then, the stored result is used in the subsequent
		// calculations. The logic for the subsequent
		// calculations proceed iteratively in the following manner. The
		// user sees the last result on the
		// screen of the calculator and enters an operation and then a number,
		// which updates the result. This time
		// onwards, the entries are operation-number, operation-number ....
		//

		// Enter 1st number
		keepTrying = true;
		do {
			try {
				System.out.print("Enter a 1st number: ");
				amount1 = input.nextDouble();
				keepTrying = false;
			} catch (InputMismatchException e) {
				input.next();
				System.out.println("Not a valid number");
				keepTrying = true;
			}
		} while (keepTrying);

		// Enter 2nd number
		keepTrying = true;
		do {
			try {
				System.out.print("Enter 2nd number: ");
				amount2 = input.nextDouble();
				keepTrying = false;
			} catch (InputMismatchException e) {
				input.next();
				System.out.println("Not valid number");
				keepTrying = true;
			}
		} while (keepTrying);

		// Enter operation
		keepTrying = true;
		do {
			try {
				System.out.print("Enter +, - , *, /, %, c: ");
				operator = input.next().charAt(0);
				if (operator != '+' && operator != '-' && operator != '*'
						&& operator != '/' && operator != '%'
						&& operator != 'c') {
					throw new InputMismatchException();
				}
				keepTrying = false;
			} catch (InputMismatchException e) {
				System.out.println("Not valid operator");
				keepTrying = true;
			}
		} while (keepTrying);

		if (operator == 'c') {
			System.out.println("Exiting calculator ....");
			System.exit(1);
		}

		calculator.claculate(amount1, amount2, operator);
		System.out.println(amount1 + " " + operator + " " + amount2 + " = "
				+ calculator.getResult());
		if (operator == '/') {
			System.out.println("and remainder = " + calculator.getRemainder());
		}

		// Basic operation with two numbers is completed. Now the iterative
		// operation with the result and
		// newly accepted number starts. While the last result is on screen,
		// user enters an operation and a
		// new number and continues.
		//

		while (true) {
			keepTrying = true;        // Enter operation
			do {
				try {
					System.out.print("Enter +, - , *, /, %, c: ");
					operator = input.next().charAt(0);
					if (operator != '+' && operator != '-' && operator != '*'
							&& operator != '/' && operator != '%'
							&& operator != 'c') {
						throw new InputMismatchException();
					}
					keepTrying = false;
				} catch (InputMismatchException e) {
					System.out.println("Not valid operator");
					keepTrying = true;
				}
			} while (keepTrying);

			if (operator == 'c') {
				System.out.println("Exiting calculator ....");
				break;
			}

			keepTrying = true;     // Enter number.
			do {
				try {
					System.out.print("Enter a number: ");
					amount1 = input.nextDouble();
					keepTrying = false;
				} catch (InputMismatchException e) {
					input.next();
					System.out.println("Not a valid number");
					keepTrying = true;
				}
			} while (keepTrying);

			calculator.claculate(amount1, operator);
			System.out.println(operator + " " + amount1 + " " + " = "
					+ calculator.getResult());
			if (operator == '/') {
				System.out.println("and remainder = "
						+ calculator.getRemainder());
			}
		}
		input.close();

	}
}
